home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / password / password.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  8.7 KB  |  300 lines

  1. /*
  2.     File:        Password.c
  3.  
  4.     Contains:    This file contains all the interesting password stuff
  5.  
  6.     Written by: Tim Dierks    
  7.  
  8.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include <Types.h>
  25. #include <Memory.h>
  26. #include <Resources.h>
  27. #include <OSUtils.h>
  28. #include <Quickdraw.h>
  29. #include <Fonts.h>
  30. #include <Events.h>
  31. #include <OSEvents.h>
  32. #include <Windows.h>
  33. #include <Menus.h>
  34. #include <Dialogs.h>
  35. #include <TextEdit.h>
  36.  
  37. #include "Sample.h"
  38. #include "Password.h"
  39.  
  40. void DisplayPassword (ConstStr255Param password)
  41. {    DialogPtr    dlog;
  42.     short        item;
  43.     //short        rDisplayPasswordDialog;
  44.     
  45.     dlog = GetNewDialog(rDisplayPasswordDialog,0L,(WindowPtr) -1L);
  46.     
  47.     ParamText((ConstStr255Param)password,0L,0L,0L);
  48.     
  49.     do
  50.     {    ModalDialog(0L,&item);
  51.     } while (item != 1);            // Until the OK button gets hit
  52.     
  53.     DisposeDialog(dlog);
  54. }
  55.  
  56. void TwoItemDialog (StringPtr password)
  57. {
  58.     DialogPtr        dlog;
  59.     Handle            itemH;
  60.     ControlHandle    chkBox;
  61.     short            item,itemType,chkVal;
  62.     Rect            box;
  63.     Point            size;
  64.  
  65.     static ModalFilterUPP twoItemFilterUPP;
  66.     
  67.     /* set up a UPP for the dialog filter */
  68.  
  69.     if (!twoItemFilterUPP)
  70.         twoItemFilterUPP = NewModalFilterProc(TwoItemFilter);
  71.     
  72.     dlog = GetNewDialog(rTwoItemDialog,0L,(WindowPtr) -1L);
  73.     
  74.     if (dlog)
  75.     {
  76.         do
  77.         {    ModalDialog(twoItemFilterUPP,&item);
  78.             if (item == 4)                                            // Hide/show checkbox
  79.             {    GetDialogItem(dlog,4,&itemType,(Handle*)&chkBox,&box);    // Get check value
  80.                 chkVal = !GetControlValue(chkBox);
  81.                 SetControlValue(chkBox,chkVal);                            // Invert it
  82.                 size.v = dlog->portRect.bottom - dlog->portRect.top;
  83.                 size.h = dlog->portRect.right - dlog->portRect.left;
  84.                 if (chkVal)
  85.                     size.v += 35;
  86.                 else
  87.                     size.v -= 35;
  88.                 
  89.                 SizeWindow(dlog,size.h,size.v,true);        // Resize window
  90.             }
  91.         } while (item != 1);            // Until the OK button is hit
  92.     
  93.         GetDialogItem(dlog,3,&itemType,&itemH,&box);        // Get text from hidden dialog item
  94.         GetDialogItemText(itemH, password);
  95.         
  96.         DisposeDialog(dlog);
  97.     }
  98. }
  99.  
  100. pascal Boolean
  101. TwoItemFilter(DialogPtr dlog,EventRecord *event,short *itemHit)
  102. {    DialogPtr    evtDlog;
  103.     short        selStart,selEnd;
  104.     
  105.     if (event->what == keyDown || event->what == autoKey)
  106.     {    switch (event->message & charCodeMask)
  107.         {    case '\n':            // Return  (hitting return or enter is the same as hitting the OK button)
  108.             case '\003':        // Enter
  109.                 *itemHit = 1;        // OK Button
  110.                 return true;        // We handled the event
  111.             case '\t':            // Tab
  112.                 event->what = nullEvent;    // Do nothing (don't let the user tab to the hidden field)
  113.                 return false;
  114.             case '\034':        // Left arrow  (Keys that just change the selection)
  115.             case '\035':        // Right arrow
  116.             case '\036':        // Up arrow
  117.             case '\037':        // Down arrow
  118.                 return false;            // Let ModalDialog handle them
  119.             default:
  120.                 selStart = (**((DialogPeek)dlog)->textH).selStart;        // Get the selection in the visible item
  121.                 selEnd = (**((DialogPeek)dlog)->textH).selEnd;
  122.                 SelectDialogItemText(dlog,3,selStart,selEnd);                // Select text in invisible item
  123.                 DialogSelect(event,&evtDlog,itemHit);            // Input key
  124.                 SelectDialogItemText(dlog,2,selStart,selEnd);                // Select same area in visible item
  125.                 if ((event->message & charCodeMask) != '\010')    // If it's not a backspace (backspace is the only key that can affect both the text and the selection- thus we need to process it in both fields, but not change it for the hidden field.
  126.                     event->message = '•';                        // Replace with character to use
  127.                 DialogSelect(event,&evtDlog,itemHit);            // Put in fake character
  128.                 return true;
  129.         }
  130.     }
  131.     
  132.     return false;            // For all non-keyDown events
  133. }
  134.  
  135. void DifferentFontDialog (StringPtr password)
  136. {
  137.     DialogPtr        dlog;
  138.     Handle            itemH;
  139.     short            item,itemType,font;
  140.     Rect            box;
  141.     UserItemUPP     chicagoTextItemUPP;
  142.     //short             rDifferentFontDialog;
  143.     
  144.     /* create the UPP for our ChicagoTextItem userItem */
  145.     chicagoTextItemUPP = NewUserItemProc(ChicagoTextItem);
  146.     
  147.     GetFNum("\p.Pwd",&font);        // Get the font number for our password font (it begins with a period, so AppendResMenu won't add it)
  148.     SetDialogFont(font);                // Use this font for static and edit text items in further dialogs
  149.     
  150.     dlog = GetNewDialog(rDifferentFontDialog,0L,(WindowPtr) -1L);
  151.     
  152.     GetDialogItem(dlog,3,&itemType,&itemH,&box);            // Because SetDialogFont affects static items, too, we've got to use user items to draw our prompts
  153.     SetDialogItem(dlog,3,itemType,(Handle)chicagoTextItemUPP,&box);
  154.     GetDialogItem(dlog,4,&itemType,&itemH,&box);
  155.     SetDialogItem(dlog,4,itemType,(Handle)chicagoTextItemUPP,&box);
  156.     
  157.     do
  158.     {    ModalDialog(0L,&item);
  159.     } while (item != 1);            // Until the OK button is hit
  160.     
  161.     GetDialogItem(dlog,2,&itemType,&itemH,&box);        // Get text from TE item
  162.     GetDialogItemText(itemH, password);
  163.     
  164.     DisposeDialog(dlog);
  165.     
  166.     SetDialogFont(0);        // Set the dialog font back to the System font
  167.     
  168.     DisposeRoutineDescriptor(chicagoTextItemUPP);
  169. }
  170.  
  171. pascal void
  172. ChicagoTextItem(WindowPtr wind,short item)
  173. {    short        fontStore,sizeStore;
  174.     Handle        itemH;
  175.     short        itemType;
  176.     Rect        box;
  177.     char        *text;
  178.     
  179.     SetPort(wind);
  180.     
  181.     fontStore = wind->txFont;    // Remember the current font & size
  182.     sizeStore = wind->txSize;
  183.     
  184.     TextFont(0);        // Set to default System font & size
  185.     TextSize(0);
  186.     
  187.     GetDialogItem(wind,item,&itemType,&itemH,&box);
  188.     
  189.     if (item == 3)            // These strings would probably be in a resource or somesuch in an actual program.
  190.         (ConstStr255Param) text = "\pPlease enter your password:";
  191.     if (item == 4)
  192.         (ConstStr255Param) text = "\pSpecial Font Password Dialog";
  193.     
  194.     TETextBox(text+1,*text,&box,teJustLeft);    // Draw the prompt
  195.     
  196.     TextFont(fontStore);        // Restore the font & size
  197.     TextSize(sizeStore);
  198. }
  199.  
  200. void InternalBufferDialog (StringPtr password)
  201. {
  202.     DialogPtr        dlog;
  203.     short            item;
  204.     ModalFilterUPP  internalBufferFilterUPP;
  205.     //short            rInternalBufferDialog;
  206.     
  207.     /* create the UPP for the InternalBufferFilter */
  208.     internalBufferFilterUPP = NewModalFilterProc(InternalBufferFilter);
  209.     
  210.     dlog = GetNewDialog(rInternalBufferDialog,0L,(WindowPtr) -1L);
  211.     
  212.     *password = '\0';                    // Zero out the buffered password
  213.     SetWRefCon(dlog,(long)password);    // Stash the buffer's address
  214.     
  215.     do
  216.     {    ModalDialog(internalBufferFilterUPP,&item);
  217.     } while (item != 1);            // Until the OK button is hit
  218.     
  219.     DisposeDialog(dlog);
  220.     
  221.     DisposeRoutineDescriptor(internalBufferFilterUPP);
  222. }
  223.  
  224. pascal Boolean
  225. InternalBufferFilter(DialogPtr dlog,EventRecord *event,short *itemHit)
  226. {    char    key;
  227.     short    start,end;
  228.     char    *buffer;
  229.     
  230.     if (event->what != keyDown && event->what != autoKey)
  231.         return false;                // We don't want to deal with them
  232.     
  233.     key = event->message & charCodeMask;
  234.     
  235.     switch (key)
  236.     {    case '\n':            // Return
  237.         case '\003':        // Enter
  238.             *itemHit = 1;        // OK Button
  239.             return true;        // We handled the event
  240.         case '\t':            // Tab
  241.         case '\034':        // Left arrow
  242.         case '\035':        // Right arrow
  243.         case '\036':        // Up arrow
  244.         case '\037':        // Down arrow
  245.             return false;        // Let ModalDialog handle them
  246.         default:            // Everything else falls through to be dealt with
  247.             break;            //    below
  248.     }
  249.     
  250.     start = (**((DialogPeek)dlog)->textH).selStart;    // Get the current selection
  251.     end = (**((DialogPeek)dlog)->textH).selEnd;
  252.     
  253.     buffer = (char*)GetWRefCon(dlog);        // Get the buffer's address
  254.     
  255.     if (start != end)                    // If there's a selection, delete it
  256.         DeleteRange(buffer,start,end);
  257.     
  258.     if (key == '\010')    // Backspace
  259.     {    if (start != 0)
  260.             DeleteRange(buffer,start-1,start);    // Delete the character to the left
  261.     }
  262.     else
  263.     {    InsertChar(buffer,start,key);        // Insert the real key into the buffer
  264.         event->message = '*';                // Character to use in field
  265.     }
  266.     
  267.     return false;         // Let ModalDialog insert the fake char
  268. }
  269.  
  270. void
  271. DeleteRange(char *buffer,short start,short end)
  272. {    register char    *src,*dest,*last;
  273.     
  274.     last = buffer + *buffer;
  275.     
  276.     src = buffer + end + 1;
  277.     dest = buffer + start + 1;
  278.     
  279.     while (src <= last)            // Shift character to the left over the removed characters
  280.         *(dest++) = *(src++);
  281.     
  282.     (*buffer) -= (end-start);    // Adjust the buffer's length
  283. }
  284.  
  285. void
  286. InsertChar(char *buffer,short pos,char c)
  287. {    register short    index,len;
  288.     
  289.     len = *buffer;
  290.     
  291.     if (len == 0xFF)        // if the string is full, return
  292.         return;
  293.     
  294.     for (index = len;index > pos;index--)    // Shift characters to the right to make room
  295.         buffer[index+1] = buffer[index];
  296.     
  297.     buffer[pos+1] = c;        // Fill in the new character
  298.     
  299.     (*buffer)++;            // Add one to the length of the string
  300. }